package DO1;

/**
 * Created by Cai Ye on 2016/4/8.
 */

import java.util.Scanner;

public class Admin {
    String msg = "\t编号\t\t姓名\tjava    汇编  3数据结构 密码学 总分";

    public void print(Student[] arr) {
        System.out.println(msg);
        for (int i = 0; i < arr.length; i++) {
            if (arr[i] != null) {
                arr[i].setSum();
                System.out.println(arr[i]);
            }
        }
    }

    public boolean exist(int num, Student stu) {
        if (stu != null) {
            return stu.getNum() == num;
        }
        return false;
    }

    public void Create(int num, String name, Student[] arr) {
        Student stu = new Student();
        stu.setNum(num);
        stu.setName(name);
        int i = this.setIndex(arr);
        if (i == 9999) {
            System.out.println("学生人数已添满，不能再添加了");
        } else {
            arr[i] = stu;
        }
        this.print(arr);
    }

    public int setIndex(Student[] arr) {
        for (int i = 0; i < arr.length; i++) {
            if (arr[i] == null) {
                return i;
            }
        }
        return 9999;
    }

    public void find(int num, Student[] arr) {
        for (int i = 0; i < arr.length; i++) {
            if (this.exist(num, arr[i]) == true) {
                System.out.println(msg);
                System.out.println(arr[i]);
                return;
            }
        }
        System.out.println("没有找到该学生信息");
    }

    public void delete(int num, Student[] arr) {
        for (int i = 0; i < arr.length; i++) {
            if (this.exist(num, arr[i])) {
                arr[i] = null;
                this.print(arr);
                return;
            }
        }
        System.out.println("没有找到该学生");
    }

    public void input(int num, Student[] arr) {
        for (int i = 0; i < arr.length; i++) {
            if (this.exist(num, arr[i])) {
                Scanner in = new Scanner(System.in);
                System.out.println("请输入" + arr[i].getName() + "java的成绩：");
                if (in.hasNextInt()) {
                    arr[i].setJava(in.nextInt());
                } else {
                    return;
                }
                System.out.println("请输入" + arr[i].getName() + "汇编的成绩：");
                if (in.hasNextInt()) {
                    arr[i].setHb(in.nextInt());
                } else {
                    return;
                }
                System.out.println("请输入" + arr[i].getName() + "数据结构的成绩：");
                if (in.hasNextInt()) {
                    arr[i].setSjjg(in.nextInt());
                } else {
                    return;
                }
                System.out.println("请输入" + arr[i].getName() + "密码学的成绩：");
                if (in.hasNextInt()) {
                    arr[i].setMmx(in.nextInt());
                } else {
                    return;
                }
                this.print(arr);
                return;
            }
        }
    }

    public void courseSort(int num, Student[] arr) {
        if (num == 1) {
            for (int i = 0; i < arr.length; i++) {
                for (int j = i + 1; j < arr.length; j++) {
                    if (arr[i] != null && arr[j] != null) {
                        if (arr[i].getJava() < arr[j].getJava()) {
                            Student t = arr[i];
                            arr[i] = arr[j];
                            arr[j] = t;
                        }
                    }
                }
            }
        } else if (num == 2) {
            for (int i = 0; i < arr.length; i++) {
                for (int j = i + 1; j < arr.length; j++) {
                    if (arr[i] != null && arr[j] != null) {
                        if (arr[i].getSjjg() < arr[j].getSjjg()) {
                            Student t = arr[i];
                            arr[i] = arr[j];
                            arr[j] = t;
                        }
                    }
                }
            }
        } else if (num == 3) {
            for (int i = 0; i < arr.length; i++) {
                for (int j = i + 1; j < arr.length; j++) {
                    if (arr[i] != null && arr[j] != null) {
                        if (arr[i].getHb() < arr[j].getHb()) {
                            Student t = arr[i];
                            arr[i] = arr[j];
                            arr[j] = t;
                        }
                    }
                }
            }
        } else if (num == 4) {
            for (int i = 0; i < arr.length; i++) {
                for (int j = i + 1; j < arr.length; j++) {
                    if (arr[i] != null && arr[j] != null) {
                        if (arr[i].getMmx() < arr[j].getMmx()) {
                            Student t = arr[i];
                            arr[i] = arr[j];
                            arr[j] = t;
                        }
                    }
                }
            }
        }
        this.print(arr);
    }

    public void sumSort(Student[] arr) {
        for (int i = 0; i < arr.length; i++) {
            for (int j = i + 1; j < arr.length; j++) {
                if (arr[i] != null && arr[j] != null) {
                    if (arr[i].getSum() < arr[j].getSum()) {
                        Student t = arr[i];
                        arr[i] = arr[j];
                        arr[j] = t;
                    }
                }
            }
        }
        this.print(arr);
    }

    public void numSort(Student[] arr) {
        for (int i = 0; i < arr.length; i++) {
            for (int j = i + 1; j < arr.length; j++) {
                if (arr[i] != null && arr[j] != null) {
                    if (arr[i].getNum() > arr[j].getNum()) {
                        Student t = arr[i];
                        arr[i] = arr[j];
                        arr[j] = t;
                    }
                }
            }
        }
        this.print(arr);
    }
}